package com.binguo.wxapi.service;

import com.binguo.model.api.vo.ProductVO;
import com.binguo.model.mapper.LogShopBaiduImgDelMapper;
import com.binguo.model.model.*;
import com.binguo.service.ProductService;
import com.binguo.service.ShopProductMsgService;
import com.binguo.service.ShopService;
import com.binguo.wxapi.baidu.BaiduRecUtil;
import com.binguo.wxapi.result.Result;
import com.binguo.wxapi.result.ResultGenerator;
import com.binguo.yprecapi.YpRecService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.binguo.common.constants.ProductConstants.*;

/**
 * @author yuritian
 * @Title: WxDeleteProductService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/6/2419:38
 */
@Service
public class WxDeleteProductService {

    @Resource
    ProductService productService;

    @Resource
    ShopService shopService;

    @Resource
    YpRecService ypRecService;

    @Resource
    BowlService bowlService;

    @Resource
    BaiduRecUtil baiduRecUtil;

    @Resource
    ShopProductMsgService shopProductMsgService;

    @Resource
    LogShopBaiduImgDelMapper logShopBaiduImgDelMapper;

    public List<ProductVO> doDeleteProduct(Product product) {


        product.setDeleted(true);
        product.setStatus(new Byte("2"));
        product.setUpdated(new Date());

        productService.updateProduct(product);

        Shop shop = shopService.findShopById(product.getShopid());

        if(shop.getProductRecType() != null && shop.getProductRecType().intValue() == SHOP_PRODUCT_REC_TYPE_YP_ONLINE) {
            ypRecService.delProductAndFiles(product.getId(), product.getStoreid(), bowlService.localRecIp);
        }

        if(shop.getProductRecType() != null && shop.getProductRecType().intValue() == SHOP_PRODUCT_REC_TYPE_YP_OFFLINE_PURE) {
            ypRecService.delProductAndFiles(product.getId(), product.getStoreid(), bowlService.localRecIp);
        }

        if(shop.getProductRecType() != null && shop.getProductRecType().intValue() == SHOP_PRODUCT_REC_TYPE_YP_OFFLINE) {
            shopProductMsgService.addDeleteProductMessage(product);
        }


        List<LogProductImg> logProductImgList = productService.getAllProductImgList(product.getId());
        if (logProductImgList != null) {
            List<Integer> idList = new ArrayList<>();
            logProductImgList.stream().forEach(p -> {
                boolean deleted ;
                if(shop.getProductRecType() != null && shop.getProductRecType().intValue() == 4) {
                    deleted = true;
//                    deleted = ypRecService.delImg(p.getImgCutNetUrl(), shop.getId(), bowlService.localRecIp);
                }else{
                    deleted = baiduRecUtil.delImg(p.getContSign(), p.getShopId(), p.getBranchId());
                }

                if(deleted){
                    idList.add(p.getId());
                }else {
                    LogShopBaiduImgDel logShopBaiduImgDel = new LogShopBaiduImgDel();
                    logShopBaiduImgDel.setLogId(p.getId());
                    logShopBaiduImgDel.setImgType(1);
                    logShopBaiduImgDel.setStatus(0);

                    logShopBaiduImgDelMapper.insertSelective(logShopBaiduImgDel);
                }
            });

            if (idList.size() > 0) {
                productService.deleteProductImgList(idList);
            }
        }

        List<LogProductImgFromUser> logProductImgFromUserList = productService.getProductImgFromUserList(product.getId());
        if (logProductImgFromUserList != null) {
            List<Integer> idList = new ArrayList<>();
            logProductImgFromUserList.stream().forEach(p -> {
                boolean deleted ;

                if(shop.getProductRecType() != null && shop.getProductRecType().intValue() == 4) {
                    deleted = true;
//                    deleted = ypRecService.delImg(p.getImgCutNetUrl(), shop.getId(), bowlService.localRecIp);
                }else{
                    if (p.getSubDb() == null || p.getSubDb() == 0) {
                        deleted = baiduRecUtil.delImg(p.getContSign(), p.getShopId(), p.getBranchId());
                    }else {
                        deleted = baiduRecUtil.delImgCurList(p.getContSign(), p.getShopId(), p.getBranchId(), p.getBaiduAppId());
                    }
                }

                if(deleted){
                    idList.add(p.getId());
                }else {
                    LogShopBaiduImgDel logShopBaiduImgDel = new LogShopBaiduImgDel();
                    logShopBaiduImgDel.setLogId(p.getId());
                    logShopBaiduImgDel.setImgType(2);
                    logShopBaiduImgDel.setStatus(0);

                    logShopBaiduImgDelMapper.insertSelective(logShopBaiduImgDel);
                }
            });

            if (idList.size() > 0) {
                productService.deleteProductImgFromUserList(idList);
            }
        }

        productService.delProductImgList(product.getId());

        productService.delProductYpRef(product.getId(), product.getShopid());

        if (product.getStoreid() == null) {
            List<ProductVO> list = productService.findProductsForWX(product.getShopid(),
                    Optional.empty(), Optional.empty());
            Collections.sort(list, Comparator.comparing(ProductVO::getPrice));
            return list;
        }else {
            List<ProductVO> list = productService.findBranchProductsForWX(product.getShopid(), Optional.empty(),
                    Optional.empty(), product.getStoreid());

            if (list == null || list.size() == 0) {
                list = productService.findProductsForWX(product.getShopid(), Optional.empty(), Optional.empty());
            }
            Collections.sort(list, Comparator.comparing(ProductVO::getPrice));
            return list;
        }
    }
}
